import { readFileSync } from 'fs';
import { fileTypeFromBuffer } from 'file-type';
async function toDataURL(filePath) {
    const data = readFileSync(filePath);
    const type = await fileTypeFromBuffer(data);
    const mime = type?.mime || 'application/octet-stream';

    return `data:${mime};base64,${data.toString('base64')}`;
}

export  async function setImage(imagePath:string){
    const base64 =  await toDataURL(imagePath)
    setImageFromBase64(base64)
}


export  function setImageFromBase64(base64:string){
    const API_KEY="dot_app_cDUaNGfbDedBbHXPWpdHdUdZNskjpPVRYULujQHwjtzRKgcpCCQMPvEILNlhyKYw"
    const DEVICE_ID="48F6EE55BC54"

    // const image =  await toDataURL("./hello.png")
    fetch("https://dot.mindreset.tech/api/open/image", {
        method: "POST",
        headers: {
            "Authorization": `Bearer ${API_KEY}`,
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            refreshNow: true,
            deviceId: DEVICE_ID,
            image: base64,
            link: "https://www.baidu.com",
            border: 0,
            ditherType:'NONE',
            // ditherType: "ORDERED"
            // ditherKernel: "THRESHOLD"
        })
    }).then(res => res.json()).then(console.log);
}
